﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
   public class ArgumentTypeXmlDAL
    {

       private  string DbName;
       //private  string CommonDbName;
       public ArgumentTypeXmlDAL()
       {
          DbName = "JudgeDoc";
          //CommonDbName = "LdtCommonManage";
       }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
       public string Exists(SYS_ArgumentTypeXml Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ATX_ID from SYS_ArgumentType_Xml");
            strSql.Append(" where XmlPath=@XmlPath ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@XmlPath", DbType.String, Model.XmlPath);

            return LdFormat.ObjectToString( oBase.ExecuteScalar(oCmd));
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
       public bool Add(SYS_ArgumentTypeXml Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SYS_ArgumentType_Xml(");
            strSql.Append("ATX_ID,CauseID,ElementID,ElementName,XmlPath,SourctType,SourceContent,AT_MatchKeyWord ,Paramter_ISCheck)");
            strSql.Append(" values (");
            strSql.Append("@ATX_ID,@CauseID,@ElementID,@ElementName,@XmlPath,@SourctType,@SourceContent,@AT_MatchKeyWord,@Paramter_ISCheck)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@ATX_ID", DbType.String, Model.ATX_ID);
            oBase.AddInParameter(oCmd, "@CauseID", DbType.String, Model.CauseID);
            oBase.AddInParameter(oCmd, "@ElementID", DbType.String, Model.ElementID);
            oBase.AddInParameter(oCmd, "@ElementName", DbType.String, Model.ElementName);
            oBase.AddInParameter(oCmd, "@XmlPath", DbType.String, Model.XmlPath);
            oBase.AddInParameter(oCmd, "@SourctType", DbType.String, Model.SourctType);
            oBase.AddInParameter(oCmd, "@SourceContent", DbType.String, Model.SourceContent);
            oBase.AddInParameter(oCmd, "@AT_MatchKeyWord", DbType.String, Model.AT_MatchKeyWord);
            oBase.AddInParameter(oCmd, "@Paramter_ISCheck", DbType.Int32, Model.Paramter_ISCheck);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


       /// <summary>
       /// 更新一条数据
       /// </summary>
       public bool Update(SYS_ArgumentTypeXml Model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("update SYS_ArgumentType_Xml set ");
           strSql.Append("CauseID=@CauseID,");
           strSql.Append("ElementID=@ElementID,");
           strSql.Append("ElementName=@ElementName,");
           strSql.Append("XmlPath=@XmlPath,");
           strSql.Append("SourctType=@SourctType,");
           strSql.Append("SourceContent=@SourceContent");
           strSql.Append(",Paramter_ISCheck=@Paramter_ISCheck");
           strSql.Append(",AT_MatchKeyWord=@AT_MatchKeyWord");
        

           strSql.Append(" where ATX_ID=@ATX_ID ");
           Database oBase = DatabaseFactory.CreateDatabase(DbName);
           DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

           oBase.AddInParameter(oCmd, "@ATX_ID", DbType.String, Model.ATX_ID);
           oBase.AddInParameter(oCmd, "@CauseID", DbType.String, Model.CauseID);
           oBase.AddInParameter(oCmd, "@ElementID", DbType.String, Model.ElementID);
           oBase.AddInParameter(oCmd, "@ElementName", DbType.String, Model.ElementName);
           oBase.AddInParameter(oCmd, "@XmlPath", DbType.String, Model.XmlPath);
           oBase.AddInParameter(oCmd, "@SourctType", DbType.String, Model.SourctType);
           oBase.AddInParameter(oCmd, "@SourceContent", DbType.String, Model.SourceContent);
           oBase.AddInParameter(oCmd, "@AT_MatchKeyWord", DbType.String, Model.AT_MatchKeyWord);
           oBase.AddInParameter(oCmd, "@Paramter_ISCheck", DbType.Int32, Model.Paramter_ISCheck);
           if (oBase.ExecuteNonQuery(oCmd) > 0)
           {
               return true;
           }
           return false;
       }

     
       /// <summary>
       /// 得到一个对象实体
       /// </summary>
       public SYS_ArgumentTypeXml GetModel(string CCID)
       {

           StringBuilder strSql = new StringBuilder();
           strSql.Append("select  top 1 * from SYS_ArgumentType_Xml ");
           strSql.Append(" where ElementID=@ElementID ");
             Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@ElementID", DbType.String, CCID);
            DataSet ds = oBase.ExecuteDataSet(oCmd);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
       }
//       where ElementID in (
//'CC1510261516360001',
//'CC1510261516360003',
//'CC1510261516360004',
//'CC1511171404250039',
//'CC1511231112480021',
//'CC1603221758440019')"
       /// <summary>
       /// 得到一个对象实体
       /// </summary>
       public DataTable GetAllModel( )
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@"select * from dbo.SYS_ArgumentType_Xml
           ");
           Database oBase = DatabaseFactory.CreateDatabase(DbName);
           DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
           DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
           if (OtB.Rows.Count > 0)
           {
               return OtB;
           }
           else
           {
               return new DataTable();
           }
       }



       /// <summary>
       /// 得到一个对象实体
       /// </summary>
       public SYS_ArgumentTypeXml DataRowToModel(DataRow row)
       {
           SYS_ArgumentTypeXml model = new SYS_ArgumentTypeXml();
           if (row != null)
           {
               if (row["ATX_ID"] != null)
               {
                   model.ATX_ID = row["ATX_ID"].ToString();
               }
               if (row["CauseID"] != null)
               {
                   model.CauseID = row["CauseID"].ToString();
               }

               if (row["ElementID"] != null)
               {
                   model.ElementID = row["ElementID"].ToString();
               }
               if (row["ElementName"] != null)
               {
                   model.ElementName = row["ElementName"].ToString();
               }
               if (row["XmlPath"] != null)
               {
                   model.XmlPath = row["XmlPath"].ToString();
               }
               if (row["SourctType"] != null)
               {
                   model.SourctType = row["SourctType"].ToString();
               }
               if (row["SourceContent"] != null)
               {
                   model.SourceContent = row["SourceContent"].ToString();
               }
               if (row["AT_MatchKeyWord"] != null)
               {
                   model.AT_MatchKeyWord = row["AT_MatchKeyWord"].ToString();
               }
           }
           return model;
       }




    }
}
